﻿function preview() {
    var $dc_fields = $('.dc_fields');
    $('#dc_field_preview').addHoverFunctions({
        customEvent: function () {

            var flag = 'false'; //flag == true  d代表不需要再继续循环
            $dc_fields.find('li').each(function () {
                var $saved = $(this).attr('saved');

                if ($saved == 'false' && flag == 'false') {
                    alert('表单编辑尚未完成，请完成后再预览');
                    flag == 'true';
                }

            });
            globalHTML = "<html><head><title>表单预览</title>"
            + "<link rel='stylesheet' href='Scripts/jquery.wysiwyg.css' type='text/css'/>"
            + "<script type='text/javascript' src='Scripts/jquery-1.6.4.min.js'></script>"
            + "<script src='Scripts/jquery.wysiwyg.js' type='text/javascript'></script>"
            + "<script src='Scripts/process.dynamic.js' type='text/javascript'></script>"
            + "<script type='text/javascript'>"
            + "$(document).ready(function () {"
            + "generateScripts();"
            + "});"
            + "</script>"
            + "</head>"
            + "<body>"
            + generateFields($dc_fields)
            + "</body></html>";
            window.open("javascript:opener.globalHTML", "POPUP");
        },
        valign: 'top'
    });
}
function generateFields($target) {
    var $c = $('<div></div>');
    var $form = $('<div style="padding:30px; border: 1px solid #CBCBCB;overflow:auto"></div>').appendTo($c);
    $target.find('li').each(function () {
        var $li = $(this);
        var data = $(this).data('av');
        var itemwidth = $li.attr('itemwidth');
        var $field = $('<div></div>').css('float', 'left').css('width', itemwidth).appendTo($form);
        var controltype = $li.attr('controltype');
        var $fieldInstance = $('<div style="float:left;width:100%"><div style="padding-right:20px">' + data[0].fieldvalue + ': </div></div>').appendTo($field);
        if (controltype == "text") {
            var $input = $('<div  style="float:left;width:100%;"><div style="padding-right:20px"><input type="text" style="width:100%" value="' + data[1].fieldvalue + '"></input></div></div>').appendTo($field);
        } else if (controltype == "richtext") {
            var $input = $('<div style="float:left;width:100%;"><div style="padding-right:20px"><textarea  style="width:100%" class="richtext-control"></textarea></div></div>').appendTo($field);
            $input.find('textarea').html(data[1].fieldvalue);
        } else if (controltype == "singleselection") {
            var $input = $('<div style="float:left;width:100%;"><div style="padding-right:20px"><div></div></div>').appendTo($field);
            var ovs = data[1].fieldvalue.split('||');
            var $container = $input.find('div').last();
            for (var i = 0; i < ovs.length; i++) {
                $('<input type="radio" value="' + ovs[i] + '" class="radio">' + ovs[i] + '</input>').appendTo($container);
            }
            if (data.length > 3 && data[3].fieldvalue == '第一项选中') {
                $container.find('input').first().attr('checked', 'checked');
            }
        } else if (controltype == "multipleselection") {
            var $input = $('<div style="float:left;width:100%;"><div style="padding-right:20px"><div></div></div>').appendTo($field);
            var ovs = data[1].fieldvalue.split('||');
            var $container = $input.find('div').last();
            for (var i = 0; i < ovs.length; i++) {
                $('<input type="checkbox" value="' + ovs[i] + '" class="checkbox">' + ovs[i] + '</input>').appendTo($container);
            }

        } else if (controltype == "slide") {

        }
    });
    return $c.html();
}

function generateScripts() {
    $('.radio').click(function () {
        var $this = $(this);
        var parent = $this.parent();
        parent.find('input').each(function () {
            $(this).removeAttr("checked");
        });
        $this.attr('checked', 'checked');
    });

    $(".richtext-control").each(function () {
        $(this).wysiwyg({
            controls: {
                strikeThrough: { visible: true },
                underline: { visible: true },

                separator00: { visible: true },

                justifyLeft: { visible: true },
                justifyCenter: { visible: true },
                justifyRight: { visible: true },
                justifyFull: { visible: true },

                separator01: { visible: true },

                indent: { visible: true },
                outdent: { visible: true },

                separator02: { visible: true },

                subscript: { visible: true },
                superscript: { visible: true },

                separator03: { visible: true },

                undo: { visible: true },
                redo: { visible: true },

                separator04: { visible: true },

                insertOrderedList: { visible: true },
                insertUnorderedList: { visible: true },
                insertHorizontalRule: { visible: true },

                h4mozilla: { visible: true && $.browser.mozilla, className: 'h4', command: 'heading', arguments: ['h4'], tags: ['h4'], tooltip: "Header 4" },
                h5mozilla: { visible: true && $.browser.mozilla, className: 'h5', command: 'heading', arguments: ['h5'], tags: ['h5'], tooltip: "Header 5" },
                h6mozilla: { visible: true && $.browser.mozilla, className: 'h6', command: 'heading', arguments: ['h6'], tags: ['h6'], tooltip: "Header 6" },

                h4: { visible: true && !($.browser.mozilla), className: 'h4', command: 'formatBlock', arguments: ['<H4>'], tags: ['h4'], tooltip: "Header 4" },
                h5: { visible: true && !($.browser.mozilla), className: 'h5', command: 'formatBlock', arguments: ['<H5>'], tags: ['h5'], tooltip: "Header 5" },
                h6: { visible: true && !($.browser.mozilla), className: 'h6', command: 'formatBlock', arguments: ['<H6>'], tags: ['h6'], tooltip: "Header 6" },

                separator07: { visible: true },

                cut: { visible: true },
                copy: { visible: true },
                paste: { visible: true }
            }
        });
    });
}